$(document).ready(function () {

    $.validator.addMethod("uniqueCode", function (value, element, params) {
        var temp = true;
        var currentEmploymentContractType;
        var id = parseInt(employmentContractTypeId, 10);
        var vcCount = employmentContractTypeList.length;
        for (var j = 0; j < vcCount; j++) {
            if (id == employmentContractTypeList[j].id) {
                currentEmploymentContractType = j;
            }
        }
        var i;
        employmentContractTypeCode = $.trim($('#employmentContractType_employmentContractTypeCode').val()).toLowerCase();
        for (i = 0; i < vcCount; i++) {

            arrayCode = employmentContractTypeList[i].code.toLowerCase();
            if (employmentContractTypeCode == arrayCode) {
                temp = false
                break;
            }
        }
        if (currentEmploymentContractType != null) {
            if (employmentContractTypeCode == employmentContractTypeList[currentEmploymentContractType].code.toLowerCase()) {
                temp = true;
            }
        }

        return temp;
    });


    $("#frmSaveEmploymentContractType").validate({

        rules: {
            'employmentContractType[employmentContractTypeCode]': {
                required: true,
                maxlength: 10,
                uniqueCode: true
            },
            'employmentContractType[employmentContractTypeName]': {
                required: true,
                maxlength: 100
            },
            'employmentContractType[employmentContractTypeDescription]': {
                maxlength: 1000
            }
        },
        messages: {
            'employmentContractType[employmentContractTypeCode]': {
                required: lang_employmentContractTypeCodeRequired,
                maxlength: lang_exceed10Chars,
                uniqueCode: lang_uniqueCode
            },
            'employmentContractType[employmentContractTypeName]': {
                required: lang_employmentContractTypeNameRequired,
                maxlength: lang_exceed100Chars
            },
            'employmentContractType[employmentContractTypeDescription]': {
                maxlength: lang_exceed1000Chars
            }
        }
    });

    if (employmentContractTypeId != "") {
        makeViewMode()
    }

    $('#btnSave').click(function () {
        if ($(this).attr('value') == lang_edit) {
            makeEditMode()
        } else {
            $('#frmSaveEmploymentContractType').submit();
        }
    })

    $('#btnCancel').click(function () {
        window.location.replace(viewEmploymentContractTypeListUrl);
    });

});

function makeViewMode() {
    $('.formInputText').attr('disabled', 'disabled')
    $('.formInputTextArea').attr('disabled', 'disabled')
    $('#radio').hide()
    $('#btnSave').attr('value', lang_edit)
}

function makeEditMode() {
    $('.formInputText').removeAttr('disabled')
    $('.formInputTextArea').removeAttr('disabled')
    $('#radio').show()
    $('#btnSave').attr('value', lang_save)
}